<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/3/7
 * Time: 10:34
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Bocheck extends Base
{
    /**
     * 脖脸皮用户列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $user_keyword = trim(input('user_keyword')); // 申请用户姓名/手机号
            $keyword = trim(input('keyword')); // 联系人姓名/手机号
            $bsc_sheng = input('sheng');
            $bsc_shi = input('shi');
            $bsc_qu = input('qu');
            $bsc_status = input('bsc_status');
            $apply_role = input('apply_role'); // 角色  3 区域加盟商  4 县级运营商  5 市级分公司
            $start_time = input('start_time'); // 申请开始时间
            $end_time = input('end_time'); // 申请结束时间
            
            $where = [];
            if(isset($user_keyword) && $user_keyword != ''){
                $where['user_username|user_phone'] = ['like', '%' . $user_keyword . '%'];
            }
            if(isset($keyword) && $keyword != ''){
                $where['bsc_user_name|bsc_user_phone'] = ['like', '%' . $keyword . '%'];
            }
            // 省
            if(isset($bsc_sheng) && $bsc_sheng != ''){
                $where['bsc_sheng'] = $bsc_sheng;
            }
            // 市
            if(isset($bsc_shi) && $bsc_shi != ''){
                $where['bsc_shi'] = $bsc_shi;
            }
            // 区
            if(isset($bsc_qu) && $bsc_qu != ''){
                $where['bsc_qu'] = $bsc_qu;
            }
            // 状态
            if(isset($bsc_status) && $bsc_status != ''){
                $where['bsc_status'] = $bsc_status;
            }
            // 角色
            if(isset($apply_role) && $apply_role != ''){
                $where['bsc_role'] = $apply_role;
            }
            // 申请时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bsc_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bsc_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bsc_time'] = ['<=', $end_time];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_shop_check')
                ->join('up_u_user', 'user_id = bsc_user_id', 'LEFT')
                ->join('up_area u1', 'bsc_sheng = u1.district_id', 'LEFT')
                ->join('up_area u2', 'bsc_shi = u2.district_id', 'LEFT')
                ->join('up_area u3', 'bsc_qu = u3.district_id', 'LEFT')
                ->where($where)
                ->count('bsc_id');// 获取总条数
            $lists = Db::table('up_bo_shop_check')
                ->join('up_u_user', 'user_id = bsc_user_id', 'LEFT')
                ->join('up_area u1', 'bsc_sheng = u1.district_id', 'LEFT')
                ->join('up_area u2', 'bsc_shi = u2.district_id', 'LEFT')
                ->join('up_area u3', 'bsc_qu = u3.district_id', 'LEFT')
                ->field('bsc_id, user_username, user_phone, bsc_status, bsc_role, bsc_shop_name, bsc_user_name, 
                bsc_user_phone, bsc_time, bsc_check_time, bsc_refuse_reason, u1.district sheng, u2.district shi, 
                u3.district qu, bsc_address')
                ->where($where)
                ->order('bsc_time desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }
    
    /**
     * 审核通过
     * @return \think\response\Json
     */
    public function applyAllow(){
        $bsc_id = input('id');
        $apply = Db::table('up_bo_shop_check')
            ->where('bsc_id', $bsc_id)
            ->field('bsc_status, bsc_role, bsc_sheng, bsc_shi, bsc_qu, bsc_lon, bsc_lat, bsc_user_id, 
            bsc_shop_name, bsc_address')
            ->find();
        if($apply['bsc_status'] != 1) return json(['code' => 100, 'msg' => '申请状态异常']);
        if($apply['bsc_role'] == 6){
            $exist = Db::table('up_bo_shop_check')
                ->where(['bsc_status' => 2, 'bsc_sheng' => $apply['bsc_sheng'], 'bsc_role' => 6])
                ->find();
        }elseif($apply['bsc_role'] == 5){
            $exist = Db::table('up_bo_shop_check')
                ->where(['bsc_status' => 2, 'bsc_shi' => $apply['bsc_shi'], 'bsc_role' => 5])
                ->find();
        }elseif ($apply['bsc_role'] == 4) {
            $exist = Db::table('up_bo_shop_check')
                ->where(['bsc_status' => 2, 'bsc_qu' => $apply['bsc_qu'], 'bsc_role' => 4])
                ->find();
        }else{
            $mile = Db::table('up_about')->where('id', 11)->value('content');
            $lon = $apply['bsc_lon'];
            $lat = $apply['bsc_lat'];
            $miles = (int)$mile;
            $exist = Db::table('up_bo_shop_check')
                ->field('ROUND(6378.138 * 2 * ASIN(SQRT(POW(SIN(('.$lat.'* PI() / 180 - bsc_lat * PI() / 180) / 2),2) + COS('.$lat.' * PI() / 180) * COS(bsc_lat * PI() / 180) * POW(SIN(('.$lon.' * PI() / 180 - bsc_lon * PI() / 180) / 2),2))) * 1000) AS distance')
                ->where(['bsc_status' => 2, 'bsc_role' => ['between', [3, 4]]])
                ->having("distance <= $miles")
                ->find();
        }
        if($exist){
            return json(['code' => 100, 'msg' => '区域范围内已有受保护店铺']);
        }else{
            Db::startTrans();
            try{
                // 修改用户信息
                /*
                $sheng_name = Db::table('up_area')->where('district_id', $apply['bsc_sheng'])->value('district');
                $shi_name = Db::table('up_area')->where('district_id', $apply['bsc_shi'])->value('district');
                $qu_name = Db::table('up_area')->where('district_id', $apply['bsc_qu'])->value('district');
                $street_name = Db::table('up_area')->where('district_id', $apply['bsc_street'])->value('district');                // 更新用户角色
                Db::table('up_u_user')
                    ->where('user_id', $apply['bsc_user_id'])
                    ->update([
                        'user_bo_role' => $apply['bsc_role'],
                        'user_shop_name' => $apply['bsc_shop_name'],
                        'user_sheng' => $apply['bsc_sheng'],
                        'user_shi' => $apply['bsc_shi'],
                        'user_qu' => $apply['bsc_qu'],
                        'user_street' => $apply['bsc_street'],
                        'user_area_str' => $sheng_name . $shi_name . $qu_name . $street_name,
                        'user_address' => $apply['bsc_address']
                    ]);
                */

                // 更新申请状态
                Db::table('up_bo_shop_check')->where('bsc_id', $bsc_id)->update(['bsc_status' => 2, 'bsc_check_time' => date('Y-m-d H:i:s')]);
                
                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch(\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }
            
        }
    }
    
    /**
     * 审核拒绝
     * @return mixed|\think\response\Json
     */
    public function applyRefuse(){
        $bsc_id = input('bsc_id');
        if(Request::instance()->isPost()){
            $reason = trim(input('reason'));
            $res = Db::table('up_bo_shop_check')
                ->where('bsc_id', $bsc_id)
                ->update([
                    'bsc_status' => 3,
                    'bsc_check_time' => date('Y-m-d H:i:s'),
                    'bsc_refuse_reason' => $reason
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        return $this->fetch('', ['bsc_id' => $bsc_id]);
    }
    
    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $bsc_ids = input('ids');

        $where = [];
        if($bsc_ids == -1){
            $user_keyword = trim(input('user_keyword')); // 申请用户姓名/手机号
            $keyword = trim(input('keyword')); // 联系人姓名/手机号
            $bsc_sheng = input('sheng');
            $bsc_shi = input('shi');
            $bsc_qu = input('qu');
            $bsc_status = input('bsc_status');
            $apply_role = input('apply_role'); // 角色  3 区域加盟商  4 县级运营商  5 市级分公司
            $start_time = input('start_time'); // 申请开始时间
            $end_time = input('end_time'); // 申请结束时间

            if(isset($user_keyword) && $user_keyword != ''){
                $where['user_username|user_phone'] = ['like', '%' . $user_keyword . '%'];
            }
            if(isset($keyword) && $keyword != ''){
                $where['bsc_user_name|bsc_user_phone'] = ['like', '%' . $keyword . '%'];
            }
            // 省
            if(isset($bsc_sheng) && $bsc_sheng != ''){
                $where['bsc_sheng'] = $bsc_sheng;
            }
            // 市
            if(isset($bsc_shi) && $bsc_shi != ''){
                $where['bsc_shi'] = $bsc_shi;
            }
            // 区
            if(isset($bsc_qu) && $bsc_qu != ''){
                $where['bsc_qu'] = $bsc_qu;
            }
            // 状态
            if(isset($bsc_status) && $bsc_status != ''){
                $where['bsc_status'] = $bsc_status;
            }
            // 角色
            if(isset($apply_role) && $apply_role != ''){
                $where['bsc_role'] = $apply_role;
            }
            // 申请时间
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['bsc_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['bsc_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['bsc_time'] = ['<=', $end_time];
            }
        }else{
            if($bsc_ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            $where['bsc_id'] = ['IN', $bsc_ids];
        }

    
        $list = Db::table('up_bo_shop_check')
            ->join('up_u_user', 'user_id = bsc_user_id', 'LEFT')
            ->join('up_area u1', 'u1.district_id = bsc_sheng', 'LEFT')
            ->join('up_area u2', 'u2.district_id = bsc_shi', 'LEFT')
            ->join('up_area u3', 'u3.district_id = bsc_qu', 'LEFT')
            ->field('bsc_status, bsc_role, bsc_shop_name, u1.district sheng, u2.district shi, u3.district qu,
            bsc_address, bsc_user_name, bsc_user_phone, bsc_time, bsc_check_time, bsc_refuse_reason, 
            user_username, user_phone')
            ->where($where)
            ->select();
    
        foreach($list as $k => $v){
            // 手机号
            $list[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
            $list[$k]['bsc_user_phone'] = "\t" . $v['bsc_user_phone'] . "\t";
            // 会员等级
            switch($v['bsc_role']){
                case 3:
                    $list[$k]['bsc_role'] = '区域加盟商';
                    break;
                case 4:
                    $list[$k]['bsc_role'] = '县级运营商';
                    break;
                case 5:
                    $list[$k]['bsc_role'] = '市级分公司';
                    break;
            }
            // 状态
            switch($list[$k]['bsc_status']){
                case 1: $list[$k]['bsc_status'] = '待审核';
                    break;
                case 2: $list[$k]['bsc_status'] = '审核通过';
                    break;
                case 3: $list[$k]['bsc_status'] = '审核拒绝';
                    break;
            }
        }
    
        $cellname = [
            ['user_username', '申请用户', 15, 'LEFT'],
            ['user_phone', '申请用户手机', 18, 'LEFT'],
            ['bsc_user_name', '联系人', 15, 'LEFT'],
            ['bsc_user_phone', '联系方式', 12, 'LEFT'],
            ['bsc_role', '申请角色', 18, 'LEFT'],
            ['bsc_status', '审核状态', 18, 'LEFT'],
            ['sheng', '省', 20, 'LEFT'],
            ['shi', '市', 20, 'LEFT'],
            ['qu', '区', 20, 'LEFT'],
            ['bsc_address', '详细地址', 30, 'LEFT'],
            ['bsc_shop_name', '店铺名称', 20, 'LEFT'],
            ['bsc_refuse_reason', '拒绝原因', 30, 'LEFT'],
            ['bsc_time', '申请时间', 20, 'LEFT'],
            ['bsc_check_time', '审核时间', 20, 'LEFT']
        ];
    
        $res = exportExcelZhy('店铺申请', 'Apply_shop', $cellname, $list);
        return json($res);
    }
    
    /**
     * 保护范围
     * @return mixed|\think\response\Json
     */
    public function protectmeter(){
        if(Request::instance()->isPost()){
            $miles = input('miles');
            $res = Db::table('up_about')->where('id', 11)->setField('content', $miles);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 11)->value('content');
        return $this->fetch('', ['content' => $content]);
    }
}